package com.wendy.vo;

import com.wendy.enums.ResultEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * Description: 响应信息主体
 * date: 2022/5/7 10:59
 *
 * @author Wendy
 * @since JDK 1.8
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@SuppressWarnings("unused")
@ApiModel(value = "R对象", description = "响应信息主体")
public class R<T> implements Serializable {
  private static final long serialVersionUID = 1L;

  @Getter
  @Setter
  @ApiModelProperty(value = "响应代码")
  private int code;

  @Getter
  @Setter
  @ApiModelProperty(value = "响应信息")
  private String msg;

  @Getter
  @Setter
  @ApiModelProperty(value = "响应数据")
  private T data;

  public static <T> R<T> ok() {
    return restResult(null, ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
  }

  public static <T> R<T> ok(T data) {
    return restResult(data, ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
  }

  public static <T> R<T> ok(String msg) {
    return restResult(null, ResultEnum.SUCCESS.getCode(), msg);
  }

  public static <T> R<T> ok(T data, String msg) {
    return restResult(data, ResultEnum.SUCCESS.getCode(), msg);
  }

  public static <T> R<T> ok(ResultEnum resultEnum) {
    return restResult(null, resultEnum.getCode(), resultEnum.getMsg());
  }

  public static <T> R<T> failed() {
    return restResult(null, ResultEnum.UNKNOWN_EXCEPTION.getCode(), ResultEnum.UNKNOWN_EXCEPTION.getMsg());
  }

  public static <T> R<T> failed(T data) {
    return restResult(data, ResultEnum.UNKNOWN_EXCEPTION.getCode(), ResultEnum.UNKNOWN_EXCEPTION.getMsg());
  }

  public static <T> R<T> failed(String msg) {
    return restResult(null, ResultEnum.UNKNOWN_EXCEPTION.getCode(), msg);
  }

  public static <T> R<T> failed(T data, String msg) {
    return restResult(data, ResultEnum.UNKNOWN_EXCEPTION.getCode(), msg);
  }

  public static <T> R<T> failed(ResultEnum resultEnum) {
    return restResult(null, resultEnum.getCode(), resultEnum.getMsg());
  }

  private static <T> R<T> restResult(T data, int code, String msg) {
    R<T> result = new R<>();
    result.setCode(code);
    result.setData(data);
    result.setMsg(msg);
    return result;
  }
}
